package com.zbw.weightlibrary.WeightSub;

import android.content.Context;
import android.os.RemoteException;

import com.imin.scalelibrary.ScaleManager;
import com.imin.scalelibrary.ScaleResult;
import com.zbw.weightlibrary.Connector.BaseControlImpl;
import com.zbw.weightlibrary.Connector.IinfoCallback;

/**
 * 作者 ：Wesley
 * 时间 ：2025-03-20 15:22
 * 这个类是干嘛的？：EScale
 * 一敏电子秤
 */
public class EScale extends BaseControlImpl {
    private final Context mContext;
    private final IinfoCallback mCallback;
    private static boolean isGetWeight = false;

    public EScale(Context context, IinfoCallback callback) {
        super(callback);
        mContext = context;
        mCallback = callback;
    }

    private ScaleManager mScaleManager;

    @Override
    public void bleConnect() {
        mScaleManager = ScaleManager.getInstance(mContext);
        mScaleManager.connectService(new ScaleManager.ScaleServiceConnection() {
            @Override
            public void onServiceConnected() {
                mCallback.onConnect("连接成功！", true);
                try {
                    mScaleManager.getData(new ScaleResult() {
                        @Override
                        public void getResult(int net, int tare, boolean b) {
                            if (isGetWeight) {
                                mCallback.onGetWeight(decimalFormat.format(net * 1.0f / 1000), b);
                            }
                        }

                        @Override
                        public void getStatus(boolean b, boolean b1, boolean b2, boolean b3) {

                        }

                        @Override
                        public void getPrice(int i, int i1, int i2, String s, String s1, boolean b, boolean b1) {

                        }
                    });
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnect() {
            }
        });

    }

    @Override
    public void bleDisconnect() {
        if (mScaleManager != null) {
            try {//取消获取数据
                mScaleManager.cancelGetData();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void getWeight() {
        isGetWeight = true;
    }

    @Override
    public void stopWeight() {
        isGetWeight = false;
    }

    @Override
    public void clearTare() {
        if (mScaleManager != null) {
            try {
                mScaleManager.tare();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void tare() {
        if (mScaleManager != null) {
            try {
                mScaleManager.tare();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void zero() {
        if (mScaleManager != null) {
            try {
                mScaleManager.zero();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

}
